package com.example.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnTouchListener, OnLongClickListener{
	private final int START_DRAG = 0;
	private final int END_DRAG = 1;
	
	private int isMoving;
	private float offset_x, offset_y;
	private boolean start_yn = true;
	
	//롱클릭용 변수
	private boolean isRed = true;
	private Button btn2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //드래그
        Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnTouchListener(this);
        
        //롱클릭
        btn2 = (Button)findViewById(R.id.button2);
        btn2.setOnLongClickListener(this);
        
       /* EditText et = (EditText)findViewById(R.id.editText1);
        et.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				Toast.makeText(MainActivity.this, "onTextChanged!!!!!", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				Toast.makeText(MainActivity.this, "beforeTextChanged!!!!!", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				Toast.makeText(MainActivity.this, "afterTextChanged!!!!!", Toast.LENGTH_SHORT).show();
			}
		});*/
        
        
        /*Button btn1 = (Button)findViewById(R.id.button1); 
        btn1.setOnClickListener(this);
        btn1.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				Toast.makeText(MainActivity.this, "onTouch!!!!!", Toast.LENGTH_SHORT).show();
				return false;
			}
		});
        
        Button btn2 = (Button)findViewById(R.id.button2); 
        btn2.setOnClickListener(this);
        
        Button btn3 = (Button)findViewById(R.id.button3); 
        btn3.setOnClickListener(this);*/
        
        /*Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "onClick!!!!!", Toast.LENGTH_SHORT).show();
			}
		});*/
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


	/*@Override
	public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
		Toast.makeText(MainActivity.this, "onKey!!!!!", Toast.LENGTH_SHORT).show();
		return false;
	}*/


	/*@Override
	public void onClick(View v) {
		
		if(v.getId() == R.id.button1){
			Toast.makeText(MainActivity.this, "onClick 111 !!!!!", Toast.LENGTH_SHORT).show();
		}else if(v.getId() == R.id.button2){
			Toast.makeText(MainActivity.this, "onClick 222 !!!!!", Toast.LENGTH_SHORT).show();
		}else if(v.getId() == R.id.button3){
			Toast.makeText(MainActivity.this, "onClick 333 !!!!!", Toast.LENGTH_SHORT).show();
		}
		
	}*/


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){//버튼 누른 시점
			if(start_yn){
				offset_x = event.getRawX();
				offset_y = event.getRawY();
				
				start_yn = false;
			}
			
			Toast.makeText(MainActivity.this, "Drag Start", Toast.LENGTH_SHORT).show();
			isMoving = START_DRAG;
		}else if(event.getAction() == MotionEvent.ACTION_UP){//버튼 눌렀다 뗀 시점
			Toast.makeText(MainActivity.this, "Drag End", Toast.LENGTH_SHORT).show();
			isMoving = END_DRAG;
		}else if(event.getAction() == MotionEvent.ACTION_MOVE){//손가락으로 드래그할때...
			if(isMoving == START_DRAG){
				v.setX((int)event.getRawX() - offset_x/2 );
				v.setY((int)event.getRawY() - offset_y/2 );
			}
		}
		return false;
	}


	@Override
	public boolean onLongClick(View arg0) {
		if(isRed){
			btn2.setBackgroundColor(Color.CYAN);
			isRed = false;
		}else{
			btn2.setBackgroundColor(Color.RED);
			isRed = true;
		}
		
		Toast.makeText(MainActivity.this, "버튼 롱클릭 됨. 색상이 "+ (isRed?"빨강":"하늘색") +"으로 변경되었습니다.", Toast.LENGTH_SHORT).show();
		
		return false;
	}


/*	@Override
	public void onClick(View arg0) {
		Toast.makeText(MainActivity.this, "onClick", Toast.LENGTH_SHORT).show();
	}*/
    
/*    public void myBtnClick(View v){
    	Toast.makeText(MainActivity.this, "onClick", Toast.LENGTH_SHORT).show();
    }*/
    
    
    
}
